package com.changanw.dynamic.thread.pool.sdk.tigger.job;

import com.alibaba.fastjson.JSON;
import com.changanw.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import com.changanw.dynamic.thread.pool.sdk.domain.model.ThreadPoolConfigEntity;
import com.changanw.dynamic.thread.pool.sdk.registry.IRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @author changanw
 * @description 线程池数据上报任务
 * @date 2024/5/16 10:15
 */
public class ThreadPoolDataReportJob {
  private final Logger logger = LoggerFactory.getLogger(ThreadPoolDataReportJob.class);
  private final IDynamicThreadPoolService dynamicThreadPoolService;

  private final IRegistry registry;
  public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
    this.dynamicThreadPoolService = dynamicThreadPoolService;
    this.registry = registry;
  }
  @Scheduled(cron = "0/20 * * * * ?")
  public void execReportThreadPoolList() {
    List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
    if (!threadPoolConfigEntities.isEmpty()){
      registry.reportThreadPool(threadPoolConfigEntities);
      logger.info("动态线程池，上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));

      for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
        registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);
        logger.info("动态线程池，上报线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
      }
    }

  }
}
